package hn.sigit.view.workflow.permits;

import hn.sigit.dao.hnd.administrative.HND_BuildingPermitDAO;
import hn.sigit.dao.hnd.administrative.HND_PermitRuleGroupDAO;
import hn.sigit.model.hnd.administrative.HND_BuildingPermit;
import hn.sigit.model.hnd.administrative.HND_BuildingPermitType;
import hn.sigit.model.hnd.administrative.HND_PermitRuleGroup;
import hn.sigit.model.hnd.administrative.HND_PermitType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.bpm.BeginTask;
import org.jboss.seam.annotations.bpm.CreateProcess;
import org.jboss.seam.bpm.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

@Name("buildingPermitHelper")
@Scope(value=ScopeType.CONVERSATION)
@AutoCreate
public class BuildingPermitHelper extends PermitHelper implements Serializable {
	private static final long serialVersionUID = -3421722459928810842L;
	
	private static List<SelectItem> permitTypeValueList;

	private final String BUILDING_PERMIT_PROCESS = "buildingPermitWorkflow";

	
	public HND_BuildingPermit getPermit() {
		if (permit == null) {
			permit = new HND_BuildingPermit();
		}
		return (HND_BuildingPermit) permit;
	}
	public void setPermit(HND_BuildingPermit permit) {
		this.permit = permit;
	}
	
	public List<TaskInstance> getTaskInstanceList() {
		return getTaskInstanceList(BUILDING_PERMIT_PROCESS);
	}

	public List<TaskInstance> getPooledTaskInstanceList() {
		return getPooledTaskInstanceList(BUILDING_PERMIT_PROCESS);
	}
	
	@End
	@CreateProcess(definition=BUILDING_PERMIT_PROCESS, processKey="#{buildingPermitHelper.permitId}")
	@Transactional
	@Override
	public String beginProcess() {
		HND_BuildingPermit hndPermit = getPermit();
		
		beginProcessAux(hndPermit);

		HND_BuildingPermitDAO.save(hndPermit);
		setPermitId(hndPermit.getId());
		

		return "building_permit_activities";
	}

	@Override
	@BeginTask
	@Begin(join=true)
	public String startTask() {
		String taskName = org.jboss.seam.bpm.TaskInstance.instance().getTask().getName();
		setPermit(HND_BuildingPermitDAO.loadBuildingPermitByID(
				Long.parseLong(ProcessInstance.instance().getKey())));
		
		setPermitId(this.permit.getId());
		setParty(this.permit.getExtParty());
		
		return taskName;
	}

	public List<SelectItem> getPermitTypeValueList() {
		if (permitTypeValueList == null) {
			permitTypeValueList = new ArrayList<SelectItem>();
			permitTypeValueList.add(new SelectItem(null, "(seleccionar)"));
			for (HND_BuildingPermitType pt : HND_BuildingPermitType.values())
				permitTypeValueList.add(new SelectItem(pt, resBundle.loadMessage(pt.name())));
		}
		return permitTypeValueList;
	}
	
	@Override
	public List<HND_PermitRuleGroup> getPermitRuleGroupList() {
		if (permitRuleGroupList == null) {
			permitRuleGroupList = HND_PermitRuleGroupDAO.loadPermitRuleGroupsByType(HND_PermitType.BUILDING_PERMIT);
		}
		return permitRuleGroupList;
	}
}

